//
//  HYLocalPhotoVideoViewController.m
//  XPlayer
//
//  Created by liguoqing on 2023/8/22.
//

#import "HYLocalPhotoVideoViewController.h"
#import "HYPhotoVideoViewModel.h"
#import "HYPhotoCollectionViewCell.h"
#import "HYVideoRoomViewController.h"

@interface HYLocalPhotoVideoViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) HYPhotoVideoViewModel *viewModel;

@property (nonatomic, copy) VideoCountBlock countBlock;

@end

@implementation HYLocalPhotoVideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationBarView.titleLabel.text = NSLocalizedString(@"我的相册", nil);
    [self configUI];
    [self bindSignal];
    [self getData];
}

- (void)configUI
{
    [self.view addEmptyViewWithPlaceImageName:@"icon_empty_content" placeTitle:NSLocalizedString(@"暂无视频数据", nil)];
    [self.view addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(NAVIGATIONBAR_HEIGHT);
        make.left.bottom.right.equalTo(self.view);
    }];
}

- (void)bindSignal
{
    @weakify(self);
    [self.viewModel.updatedContentSignal subscribeNext:^(id x) {
        @strongify(self);
        if([x count]){
            if(self.countBlock){
                self.countBlock([x count]);
            }
            [self.collectionView reloadData];
        }else{
            [HYHub showMessage:NSLocalizedString(@"本地相册暂无视频", nil)];
        }
    }];
    // 同步完成
    [self.viewModel.syncSubject subscribeNext:^(id x) {
        [HYHub dismiss];
    }];
}

- (void)getData
{
    [HYHub loadMessage:NSLocalizedString(@"icloud视频同步中...", nil)];
    /// 获取相册权限
    [self.viewModel fetchPhotoVideoAuthAndSources];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [self.viewModel numberOfRowsInSection];
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HYPhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HYPhotoCollectionViewCell" forIndexPath:indexPath];
    [cell bindViewModel:[self.viewModel cellForRowAtIndexPath:indexPath]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if([self.viewModel isCanPlay:indexPath]){
        [self playLocalPhotoVideo:[self.viewModel didSelectRowAtIndexPath:indexPath]];
    }else{
        if([HYNetWorkManager shareManager].currentReachabilityStatus == NotReachable){
            [HYHub showMessage:NSLocalizedString(@"当前是icloud视频,请连接网络", nil)];
        }else{
            [HYHub showMessage:NSLocalizedString(@"icloud视频同步中...", nil)];
        }
    }
}

- (void)playLocalPhotoVideo:(HYVideoViewModel *)viewModel
{
    HYVideoRoomViewController *basePlayerVC = [[HYVideoRoomViewController alloc]initWithViewModel:viewModel];
    [self.navigationController pushViewController:basePlayerVC animated:YES];
}

- (void)photoVideoCountBlock:(VideoCountBlock)countBlock
{
    self.countBlock = countBlock;
}

- (UICollectionView *)collectionView
{
    if(!_collectionView){
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake(kItemWidth(3), kItemHeight(3));
        layout.minimumLineSpacing = kItemSpace;
        layout.minimumInteritemSpacing = kItemSpace;
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = AppBgThemeColor;
        _collectionView.contentInset = UIEdgeInsetsMake(kItemSpace, kItemSpace, kItemSpace, kItemSpace);
        [_collectionView registerClass:[HYPhotoCollectionViewCell class] forCellWithReuseIdentifier:@"HYPhotoCollectionViewCell"];
    }
    return _collectionView;
}

- (HYPhotoVideoViewModel *)viewModel
{
    if(!_viewModel){
        _viewModel = [[HYPhotoVideoViewModel alloc]init];
    }
    return _viewModel;
}

@end
